package com.liutaw.nanjingair.presenter;

import com.liutaw.nanjingair.utils.DowloaderTask;
import com.liutaw.nanjingair.utils.FileUtils;
import com.liutaw.nanjingair.utils.NetWorkUtils;
import com.liutaw.nanjingair.views.activities.products.IImportantWeatherView;

import java.io.File;

/**
 * Created by liutao on 16/3/16.
 */
public class PDFViewerPresenter implements IPDFViewerPresenter {
    public static final String PDF_FILE_NAME = "nanjingtrending.pdf";
    private IImportantWeatherView ipdfReaderController;

    public PDFViewerPresenter(IImportantWeatherView ipdfReaderController) {
        this.ipdfReaderController = ipdfReaderController;
    }

    @Override
    public void requestDownloadFile(String url, boolean isNeedRemind) {
        if (isNeedRemind && NetWorkUtils.isMobileData(ipdfReaderController.getContext())) {
            ipdfReaderController.onMobileDataRemind();
            return;
        }
        File cacheDir = FileUtils.getExternalCacheDir(ipdfReaderController.getContext());
        File pdfFile = new File(cacheDir.getAbsolutePath() + PDFViewerPresenter.PDF_FILE_NAME);
        if (pdfFile.exists()) {
            //PDF存在
            ipdfReaderController.onDownloadPdfFileCompleted(pdfFile);
        } else {
            //PDF不存在
            DowloaderTask dowloaderTask = new DowloaderTask(ipdfReaderController);
            dowloaderTask.execute(url, cacheDir.getAbsolutePath(), PDFViewerPresenter.PDF_FILE_NAME);
        }
    }


}
